package com.orhon.smartcampus.modules.baseinfo.fetcher;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.baseinfo.dao.SemesterDao;
import com.orhon.smartcampus.modules.baseinfo.entity.Semester;
import com.orhon.smartcampus.modules.baseinfo.gqlModel.GQLSemester;
import com.orhon.smartcampus.modules.baseinfo.mm.SemesterMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.R;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class SemestersFetcher implements DataFetcher {


	@Autowired
	SemesterDao semesterDao;

	@Autowired
	TokenService tokenService;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

		String token = tokenService.getSystemToken();
		if(token==null) {
			return  R.NoLogin();
		}
		Long schoolId = JwtUtils.getSchoolId(token);

		List<GQLSemester> semesters = new ArrayList<>();

		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from Semester where deleted_at = null and school_id = " + schoolId;

		if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));

			if(map.get("yearly_code")!=null) {
				hql += "and yearly_code "+ Util.decide(map.get("yearly_code"));
			}
			if(map.get("name")!=null) {
				hql += "and semester_name "+ Util.decide(map.get("name"));
			}
			if(map.get("semester_status")!=null) {
				hql += "and semester_status "+ Util.decide(map.get("semester_status"));
			}
			if(map.get("school_id")!=null) {
				hql += "and school_id " + Util.decide(map.get("school_id"));
			}
			if(map.get("is_current")!=null) {
				hql += "and is_current " + Util.decide(map.get("is_current"));
			}

		}
		hql += "order by created_at DESC";

		List<Semester> list = semesterDao.find(hql,null,page.getPage(),page.getLimit());

		for (Semester item : list) {
			GQLSchool gqlSchool= null;
			if (env.getSelectionSet().contains("data/school")){
				Schools schools = item.getBaseSchoolsBySchoolId();
				gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
			}
			GQLSemester semester = SemesterMapper.MAPPER.toSemester(item);
			semester.setSchool(gqlSchool);
			semesters.add(semester);
		}
		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = semesterDao.getTotalCount(hql);
		}

		return Result.ok().put("data" , semesters).put("total" , total);
	}


}
